package com.shangxia.monomer.study.math.algorithm.sort;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.study.math.algorithm.sort
 * @date 2021/7/21 15:28
 */
public class Bubblesort {
    public static void main(String[] args) {
        int[] arr = {2,3,5,7,9,4,1,8,6};
        System.out.println(JSONObject.toJSONString(arr));
        bubbleSort(arr);
        System.out.println(JSONObject.toJSONString(arr));
    }

    public static void bubbleSort(int[] arr) {
        int lastIndex = arr.length - 1;

        for (int j = 0; j < lastIndex; j++) {
            for (int i = 0; i < lastIndex - j; i++) {
                if (arr[i] > arr[i + 1]) {
                    int tmp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = tmp;
                }
            }
        }
    }
}
